package com.lt.tccdemo.controller;

import com.lt.tccdemo.db108.model.User;
import com.lt.tccdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * @author lucine
 * @version 1.0 2021/10/7 15:50
 */
@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("userList")
    public String userList(ModelMap modelMap){
        List<User> allUsers = userService.getAllUsers();
        modelMap.addAttribute("users",allUsers);
        return "user/user-list";
    }

    @RequestMapping("userDetail")
    public String userDetail(@RequestParam Integer userId,ModelMap modelMap){
        User user = userService.selectById(userId);
        modelMap.addAttribute("user",user);
        return "user/user-detail";
    }

    @RequestMapping("delUser")
    @ResponseBody
    public Map<String,Object> delUser(@RequestParam Integer userId){
        int result = userService.delUser(userId);
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("status",result);
        return resultMap;
    }

    /**
     * 通过版本号来保证幂等
     * @param user
     * @return
     */
    @RequestMapping("updateUser")
    public String updateUser(User user){
        userService.updateUser(user);
        return "redirect:/user/userList";
    }

    /**
     * 通过版本号来保证幂等
     * @param user
     * @return
     */
    @RequestMapping("insertUser")
    public void  insertUser(User user){
        userService.insertUser(user);
//        return "redirect:/user/userList";
    }



}
